package com.zhonglz.util.tools;

import com.zhonglz.model.SysMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class TreeList {

	/**
	 * 需要递归的集合
	 * @param menuList
	 * @return
	 */
	public static List<SysMenu> getChildMenuList(List<SysMenu> menuList){
		List<SysMenu> resultList=new ArrayList<>();
		//获取顶层元素集合
		String parentId = "";
		for (SysMenu menuInfo: menuList) {
			parentId = menuInfo.getMenuParentNo();
			if(ObjectUtils.isEmpty(parentId)){
				resultList.add(menuInfo);
			}
		}
		//获取每个顶层元素的子数据集合
		for (SysMenu menuInfo: resultList) {
			menuInfo.setChildren(getSubList(menuInfo.getMenuNo(),menuList));
		}
		return resultList;
	}

	/***
	 * 递归
	 * @param id
	 * @param entityList
	 * @return
	 */
	private  static List<SysMenu> getSubList(String id, List<SysMenu> entityList) {
		List<SysMenu> childList=new ArrayList<>();
		String parentId;
		//子集的直接子对象
		for (SysMenu entity : entityList) {
			parentId=entity.getMenuParentNo();
			if(id.equals(parentId)){
				childList.add(entity);
			}
		}
		//子集的间接子对象
		for (SysMenu entity : childList) {
			entity.setChildren(getSubList(entity.getMenuNo(), entityList));
		}

		//递归退出条件
		if(childList.size()==0){
			return null;
		}
		return childList;
	}

}
